package Thread;

import java.util.Scanner;

public class test3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int t = in.nextInt();
            while(t > 0) {
                t--;
                int n = in.nextInt(),k = in.nextInt();
                in.nextLine();
                String s = in.nextLine();
                int ret = 0;//记录最后星数
                int count = 0;//记录连胜次数
                for(int i = 0;i < n;i++) {
                    if(s.charAt(i) == 'W') {
                        count++;
                        if(count >= 3) ret += k;
                        else ret++;
                    }
                    else {
                        count = 0;
                        ret--;
                    }
                }
                System.out.println(ret);
            }
        }
    }
}
